#include "WebPages.h"
#include <iostream>

using namespace tinyxml2;

WebPage::WebPage(int id, size_t pos, size_t len, SplitTool *cut)
    : _docid(id), _docSummary(), _cuttor(cut) {
    loadPage(_docid, pos, len);
    processDoc();
}

int WebPage::getDocid() {
    return _docid;
}

string WebPage::getTitle() {
    return _docTitle;
}

string WebPage::getSummary() {
    return _docSummary;
}

map<string, int> &WebPage::getWordsMap() {
    // undo
    return _wordsMap;
}

bool operator==(const WebPage &lhs, const WebPage &rhs) {
    // undo
    return false;
}

bool operator<(const WebPage &lhs, const WebPage &rhs) {
    // undo
    return false;
}

// ostream &operator<<(ostream &os, const WebPage &page) {
//     os << "<doc>" << "\n";
//     os << "    <docid>" << page._docid << "</docid>" << "\n";
//     os << "    <title>" << page._docTitle << "</title>" << "\n";
//     os << "    <link>" << page._docUrl << "</link>" << "\n";
//     os << "    <content>" << page._docContent << "</content>" << "\n";
//     os << "</doc>" << "\n";
//     return os;
// }

void WebPage::processDoc() {
    vector<string> words = _cuttor->cut(_docContent);
    for (const auto &w : words) {
        ++_wordsMap[w];
    }

    vector<std::pair<string, int>> wordFrequency(_wordsMap.begin(), _wordsMap.end());
    std::sort(wordFrequency.begin(), wordFrequency.end(), [](const std::pair<string, int> &a, const std::pair<string, int> &b) {
        return a.second > b.second;
    });

    for (size_t i = 0; i < TOPK_NUMBER && i < wordFrequency.size(); ++i) {
        _topWords.push_back(wordFrequency[i].first); // 将高频词添加到_topWords中
    }

    // 组成句子
    vector<string> _sentences;

    string sentence;
    for (char ch : _docContent) {
        if (ch == '.' || ch == '?' || ch == '!') {
            // 如果句子非空且没有连在一起的多个标点
            if (!sentence.empty()) {
                _sentences.push_back(sentence);
                sentence.clear(); // 清空当前句子，准备接下来的句子
            }
        } else {
            // 句子中的其他字符加入到当前句子中
            sentence += ch;
        }
    }

    if (!sentence.empty()) {
        _sentences.push_back(sentence);
    }

    std::vector<std::string> selectedSentences;
    for (const auto &sentence : _sentences) {
        // 将每个句子分词，检查是否包含高频词
        bool containsTopWord = false;
        std::vector<std::string> sentenceWords = _cuttor->cut(sentence); // 分词

        for (const auto &word : sentenceWords) {
            if (find(_topWords.begin(), _topWords.end(), word) != _topWords.end()) {
                containsTopWord = true;
                break; // 如果句子中有高频词，则选中这个句子
            }
        }

        if (containsTopWord) {
            _docSummary += sentence + ".";
        }
    }
}

void WebPage::loadPage(int docid, size_t pos, size_t len) {
    string weblib = "/home/kyle/Project/WebDisk/data/newripepage.dat";
    ifstream ifs(weblib);
    ifs.seekg(pos, std::ios::beg);
    char buf[len + 1] = {0};
    ifs.read(buf, len);

    // 处理读取的数据 (此处仅打印为示例)
    std::string content(buf);
    ifs.close();

    XMLDocument xml_doc;
    xml_doc.Parse(content.c_str());
    // 检查解析是否成功
    if (xml_doc.Error()) {
        std::cerr << "Error parsing XML: " << xml_doc.ErrorStr() << "\n";
        std::cerr << content << "\n";
        return;
    }

    // 获取根元素
    XMLElement *root_doc = xml_doc.RootElement();
    // 读取content元素的内容
    _docTitle = root_doc->FirstChildElement("title")->GetText();
    _docUrl = root_doc->FirstChildElement("link")->GetText();
    _docContent = root_doc->FirstChildElement("content")->GetText();
}